# -*- coding: utf-8 -*-
import datetime
import json
import time

import scrapy

from WechatofficalSpider.items import WechatofficalspiderItem


class WechatofficalSpider(scrapy.Spider):
    name = 'wechatoffical'
    allowed_domains = ['weixin.qq.com']
    start_urls = [
        "https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=MjM5MzI1MDE0MA==&f=json&offset=0&count=10&is_ok=1&scene=124&uin=TgzMg%3D%3D&key=3338f5f18d02dc0fe86097de0a21c9b98d4b23b36f9eb3006ff10613c9814dab12a37686dae99df76c4293643d6345922e77e6a0fb52394ab62d16044be958640c07f70a9de8d461925384fd9d023840&pass_ticket=O2ABN2zSV0xr&wxtoken=&appmsg_token=1045_ZaEyxQ~~&x5=0&f=json"]

    def parse(self, response):
        try:
            jsons = json.loads(response.body_as_unicode())  # 格式化成json数据
            can_msg_continue = jsons['can_msg_continue']  # 获取是否还有数据
            next_offset = jsons['next_offset'] - 1  # 获取下一页的坐标
            general_msg_list = json.loads(jsons['general_msg_list'])['list']  # 获取需要的内容
            if (len(general_msg_list) == 0):  # 判断是否有数据没有就不继续执行
                return
            for i in range(len(general_msg_list)):  # 循环遍历所有的数据
                datas = general_msg_list[i]  # 获取数据节点
                date = time.localtime(datas['comm_msg_info']['datetime'])  # 获取时间
                date = time.strftime('%Y-%m-%d %H:%M:%S', date)  # 把时间戳进行格式化成时间格式
                app_msg_ext_info = datas['app_msg_ext_info']  # 获取文章数据

                # print(date)  # 打印日期
                # print(app_msg_ext_info['title'])  # 打印标题
                # print(app_msg_ext_info['cover'])  # 打印文章图片链接
                item = WechatofficalspiderItem()
                item['date'] = date
                item['title'] = app_msg_ext_info['title']
                item['content'] = app_msg_ext_info['content_url']
                item['cover'] = app_msg_ext_info['cover']
                # items.append(item)
                yield item
                # print('=============================================================')  # 分割没个文章
                multi_app_msg_item_list = app_msg_ext_info['multi_app_msg_item_list']  # 获取在同一段时间内发出的所有文章数据
                for i in range(len(multi_app_msg_item_list)):  # 遍历同一段时间内发出的所有文章数据
                    # print(date)  # 打印日期
                    # print(multi_app_msg_item_list[i]['title'])
                    # print(multi_app_msg_item_list[i]['cover'])

                    item = WechatofficalspiderItem()
                    item['date'] = date
                    item['title'] = app_msg_ext_info['title']
                    item['content'] = app_msg_ext_info['content_url']
                    item['cover'] = app_msg_ext_info['cover']
                    # items.append(item)
                    yield item
                    # print('=============================================================')
            if can_msg_continue == 1:  # 判断有没有数据了  有数据就添加到队列   并把数据回调设置成当前回调
                url = response.urljoin(
                    'https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=MjM5MzI1MDE0MA==&f=json&offset=' +
                    str(next_offset) +
                    '&count=10&is_ok=1&scene=124&uin=MjD&key=3338f5f18d02dc0fe86097de0a21c9b98d4b23b36f9eb3006ff10613c9814dab12a37686dae99df76c4293643d6345922e77e6a0fb52394ab62d16044be958640c07f70a9de8d461925384fd9d023840&pass_ticket=O2ABvPT9gvYfpIgFnyysOZJudf%2BxGk8br&wxtoken=&appmsg_token=1045_NQ%252JRwzeg~~&x5=0&f=json')
                yield scrapy.Request(url, callback=self.parse)
        except Exception as e:
            print(e)
            pass
